﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace speakerReco
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
        }
        private double alpha;
        private bool stripsl, realonly, powmg, usefirst, gethighfr;
        private int winsize, overlap;
        public double Alpha
        {
            get { return alpha; }
            set { alpha = value; }
        }

        public bool Stripsl
        {
            get { return stripsl; }
            set { stripsl = value; }
 
        }

        public bool RealOnly
        {
            get { return realonly; }
            set { realonly = value; }
        }

        public bool PowMag
        {
            get {return powmg;}
            set { powmg = value; }
        }
        public bool UseFirstco
        {
            get { return usefirst; }
            set { usefirst = value; }
        }
        public bool GetHighFreq
        {
            get { return gethighfr; }
            set { gethighfr = value; }
        }
        public int WinSize
        {
           get{ return winsize;}
            set { winsize = value; }
        }
        public int Overlap
        {
            get {return overlap;}
            set { overlap = value; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ConfigFileDialog.ShowDialog();
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            
            createXML();
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            Settingsstriplable.Text = "";
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            Settingsstriplable.Text = "Save The Current Configuration To File";
        }

        private void ConfigFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            ExtractXmlFile(ConfigFileDialog.FileName);
            alpha =(double) numericUpDown_Alpha.Value;
            WinSize = int.Parse(comboBox_WindwSize.Text);
            Overlap = int.Parse(comboBox_Overlaps.Text.Split('%')[0]);
            Stripsl = checkBox_StripSilence.Checked;
            RealOnly = checkBox_FFT.Checked;
            PowMag = checkBox_Powermagnitude.Checked;
            UseFirstco = checkBox_MFCC_AC.Checked;
            GetHighFreq = checkBox_MFCC_High.Checked;
        }

        public void createXML()
        {
            //if (!File.Exists(saveFileDialog1.FileName))
            //    File.Create(saveFileDialog1.FileName);
            XmlTextWriter writer = new XmlTextWriter(saveFileDialog1.FileName, Encoding.ASCII);
            writer.Formatting = Formatting.Indented;
            // <?xml version="1.0" encoding="utf-8"?>
            writer.WriteStartDocument();
            writer.WriteStartElement("Configuration");//<Configuration>
            writer.WriteStartElement("File");
            writer.WriteAttributeString("Name", saveFileDialog1.FileName);
            //writer.WriteElementString("WindowType", "0");
            writer.WriteElementString("Alpha", numericUpDown_Alpha.Value.ToString());
            writer.WriteElementString("WinSize", comboBox_WindwSize.Text);
            writer.WriteElementString("Overlap", comboBox_Overlaps.Text);
            //writer.WriteElementString("UsingHamming", "0");
            writer.WriteElementString("UseStripSilence", checkBox_StripSilence.Checked.ToString());
            writer.WriteElementString("RealOnly", checkBox_FFT.Checked.ToString());
            //writer.WriteElementString("UsingMelFilter", "0");
            writer.WriteElementString("PowMagnitude", checkBox_Powermagnitude.Checked.ToString());
            writer.WriteElementString("UseFirstCoeffAC", checkBox_MFCC_AC.Checked.ToString());
            writer.WriteElementString("GetHighFreq", checkBox_MFCC_High.Checked.ToString());

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            //writer.WriteElementString("PRODUCT", "SNB_MB_42_" + packege);//<PRODUCT>SNB_MB_42_PGA</PRODUCT>
        }

        public void ExtractXmlFile(String Filepath)
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(Filepath);
            XmlNode Attributes = Xml.SelectSingleNode("Configuration/File");
            XmlNodeList nodes = Xml.SelectNodes("Configuration/File");
            foreach (XmlNode node in nodes)
            {
                numericUpDown_Alpha.Value = decimal.Parse(node.SelectSingleNode("Alpha").InnerText);
               comboBox_WindwSize.Text=node.SelectSingleNode("WinSize").InnerText;
               comboBox_Overlaps.Text=node.SelectSingleNode("Overlap").InnerText;
               checkBox_StripSilence.Checked=bool.Parse(node.SelectSingleNode("UseStripSilence").InnerText);
               checkBox_FFT.Checked=bool.Parse(node.SelectSingleNode("RealOnly").InnerText);
               checkBox_Powermagnitude.Checked = bool.Parse(node.SelectSingleNode("PowMagnitude").InnerText);
               checkBox_MFCC_AC.Checked=bool.Parse(node.SelectSingleNode("UseFirstCoeffAC").InnerText);
               checkBox_MFCC_High.Checked=bool.Parse(node.SelectSingleNode("GetHighFreq").InnerText); 
            }
           

 
        }
    }
}
